import { 
  AmbientLight,
  DirectionalLight,
  Mesh,
  MeshLambertMaterial,
  MeshStandardMaterial,
  ObjectLoader,
  PerspectiveCamera,
  PlaneGeometry,
  Scene, 
  SpotLight, 
  TextureLoader, 
  WebGLRenderer
} from "three";
import stats from '../utils/stats';
// 调用
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { GUI } from 'dat.gui';
import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader";

let scene = new Scene();
let renderer = setRenderer();
let camera = setCamera();

setLight();
init();

const trackballControls = new OrbitControls(camera, renderer.domElement);
// 调整场景位置
trackballControls.target.set(0, 3, 0);


function render() {
  stats.update();
  trackballControls.update();

  requestAnimationFrame(render);
  renderer.render(scene, camera);
}
render();

function setRenderer() {
  const renderer = new WebGLRenderer({ antialias: true });
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  // 支持阴影生成 
  renderer.shadowMap.enabled = true;
  document.body.appendChild(renderer.domElement);

  return renderer
}

function setCamera(): PerspectiveCamera {
  let camera = new PerspectiveCamera(45, window.innerWidth/window.innerHeight, 1, 10000);
  camera.position.set(700, 200, -500);
  scene.add( camera );

  return camera;
}

// 灯光设置
function setLight() {
  let light = new DirectionalLight(0xaabbff, 0.3);
  light.position.set(300, 250, -500);
  scene.add(light);
}

/** 功能区 */
function init() {
  let objLoader = new ObjectLoader();
  objLoader.load(
    '/models/lightmap/lightmap.json',
    group => {
      scene.add(group);
    }
  )

  let loader = new OBJLoader();
  loader.load(
    '/models/baymax/Bigmax_White_OBJ.obj',
    mesh => {
      console.log(mesh);
      mesh.scale.set(0.25, 0.25, 0.25);



      scene.add(mesh);
    }
  )
}


function addGUI(mesh: MeshStandardMaterial): void {
  const controls = {
    displacementScale: 0,
    displacementBias: 0,
  }
  const gui = new GUI();  
  gui.add(controls, 'displacementScale', -5, 5, 0.01).onChange(val => {
    mesh.displacementScale = val
  });
  gui.add(controls, 'displacementBias', -5, 5, 0.01).onChange(val => {
    mesh.displacementBias = val
  })
}